<?php
namespace api\common\helpers;


/**
 * author:hollis
 * des:
 * datetime:2020 2020-04-17 14:47
 */
class ExportCsv{

    protected $titles;

    /**
     * author:hollis
     * des:构造对象
     * datetime:2020 2020-04-17 19:19
     */
    public function makeFp($titles=[],$fileName){
        set_time_limit ( 0 );
        ini_set('memory_limit', '1024M'); //设置程序运行的内存
        ini_set('max_execution_time', 0); //设置程序的执行时间,0为无上限
        ob_end_clean();  //清除内存
        ob_start();
        header("Content-Type: text/csv");
        header("Content-Disposition:filename=" . $fileName . '.csv');
        $fp = fopen('php://output', 'w');
        fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));
        $this->titles = $titles;
        fputcsv($fp, $this->titles);
        return $fp;
    }

    /**
     * author:hollis
     * des:写入数据
     * datetime:2020 2020-04-17 19:19
     */
    public function exportData($fp,$list)
    {
        foreach ($list as $key => $line) {
            fputcsv($fp, $line);
        }
        ob_flush();//清除内存
        flush();
    }


    /**
     * author:hollis
     * des:结构
     * datetime:2020 2020-04-17 19:19
     */
    public function end(){
        ob_end_clean();
        exit();
    }

}